package com.example.admin.myapplication;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Scroller;
import android.widget.Toast;

/**
 * Created by admin on 2015/9/22.
 */
public class BaseLayout extends RelativeLayout {
    private Scroller scroller;
    private int duration = 1500;
    private boolean isMove = false;
    private boolean isOpen = true;
    private Activity a;

    public BaseLayout(Context context) {
        super(context);
        a = (Activity) context;
        init();
    }

//    public BaseLayout(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }
//
//    public BaseLayout(Context context, AttributeSet attrs) {
//        super(context, attrs);
//    }

    public void init() {
        setBackgroundResource(R.color.hei);
        scroller = new Scroller(getContext());
//        setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    }


    private void startScroll() {
        int x = getScrollX();
//        int i = x+getLeft();
        scroller.startScroll(x, 0, -x, 0);
        invalidate();
        isOpen = true;
    }

    private void endScroll() {
        int x = getScrollX();
        scroller.startScroll(x, 0, -(getWidth() + x), 0);
        invalidate();
        isOpen = false;
    }

    @Override
    public void computeScroll() {
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), scroller.getCurrY());

            setBackgroundColor(Color.argb(Math.abs((int) (((float) scroller.getCurrX() / getWidth()) * 255 + 255)), 0, 0, 0));
            postInvalidate();

            if (scroller.isFinished() && !isOpen) {
                a.finish();
            }
        }
    }

    private int down;

    private int up;

    private int move;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                move = 1;
                down = (int) event.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:

                if (isOpen) {
                    move = down - (int) event.getRawX();

                    if (move > 0) {
                        down = (int) event.getRawX();
                    } else {
                        scrollTo(move, 0);
                        setBackgroundColor(Color.argb(Math.abs((int) (((float) Math.abs(move) / getWidth()) * 255 - 255)), 0, 0, 0));
//                        Log.d("test", "" +  Math.abs((int)(((float)Math.abs(move)/getWidth())*255-255)));
                    }
                } else {

                }


                break;
            case MotionEvent.ACTION_UP:
                up = (int) event.getRawX();
                if (-move > getWidth() / 3) {
                    endScroll();
                } else {
                    startScroll();
                }
                break;
        }
        return true;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                down = (int) ev.getRawX();
                break;
            case MotionEvent.ACTION_MOVE:
                move = down - (int) ev.getRawX();
                if (-move > ViewConfiguration.get(getContext()).getScaledTouchSlop()) {
                    return true;
                }
                break;

        }

        return false;
    }
}
